package com.fowo.api.controller.shipment.wfs;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.controller.purchase.vo.WarehouseBinParam;
import com.fowo.api.controller.purchase.vo.WarehouseBinStockVo;
import com.fowo.api.controller.purchase.vo.warehouseBinInventory;
import com.fowo.api.entity.Platformshipping;
import com.fowo.api.entity.PlatformshippingItem;
import com.fowo.api.entity.Product;
import com.fowo.api.entity.WarehouseBin;
import com.fowo.api.inventory.entity.InventoryPlacement;
import com.fowo.api.inventory.service.impl.InventoryPlacementServiceImpl;
import com.fowo.api.mapper.WarehouseBinDMapper;
import com.fowo.api.mapper.WarehouseBinMapper;
import com.fowo.api.model.platformshipping.PlatformshippingAllocateInventoryAction;
import com.fowo.api.service.PlatformshippingItemService;
import com.fowo.api.service.impl.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
public class platformshippingAllocateInventoryAction implements PlatformshippingAllocateInventoryAction {

    @Resource
    private PlatformshippingServiceImpl platformshippingService;
    @Resource
    private WarehouseBinDServiceImpl warehouseBinDService;
    @Resource
    protected PlatformshippingItemServiceImpl platformshippingItemService;
    @Resource
    private WarehouseBinDMapper warehouseBinDMapper;
    @Resource
    private WarehouseBinMapper warehouseBinMapper;
    @Resource
    private ProductServiceImpl productService;
    @Resource
    private InventoryPlacementServiceImpl inventoryPlacementService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            handle(idList.get(i));
        }
    }
    //分配库存
    private void handle(Long aLong) throws Exception {
        Platformshipping byId = platformshippingService.getById(aLong);

        WarehouseBinParam search=new WarehouseBinParam();
        search.setWarehouseId(byId.getDeliverywarehouse());

        List<PlatformshippingItem> list = platformshippingItemService.list(Wrappers.<PlatformshippingItem>query().lambda().eq(PlatformshippingItem::getParentPlatformshipping, byId.getId()));

       for(int i=0;i<list.size();i++){
           PlatformshippingItem platformshippingItem = list.get(i);
           search.setShopId(platformshippingItem.getShippingwarehousestore());
           search.setProductId(platformshippingItem.getProductName());
           search.setType(5L);
           List<warehouseBinInventory> binByWarehouse = getBinByWarehouse(search);
           Assert.isTrue(binByWarehouse.size()>0,platformshippingItem.getSku()+"仓位不存在");
           platformshippingItem.setBinid(Long.valueOf(binByWarehouse.get(0).getId()));
           platformshippingItemService.updateById(platformshippingItem);
       }
        byId.setShippingstatus("0");
        platformshippingService.updateById(byId);
        //锁定库存
        platformshippingService.lockpInventory(aLong);
        platformshippingService.locksInventory(aLong);
        //设置锁库村
        byId.setIsinventorylocked("0");
        //修改为代发货
        byId.setShippingstatus("1");
        platformshippingService.updateById(byId);
    }

    public List<warehouseBinInventory> getBinByWarehouse(WarehouseBinParam search) throws Exception {
        WarehouseBinParam warehouseBinParam =new WarehouseBinParam();
        warehouseBinParam.setShopId(search.getShopId());
        warehouseBinParam.setType((long) 5);
        warehouseBinParam.setProductId(search.getProductId());
        warehouseBinParam.setWarehouseId(search.getWarehouseId());
        List<warehouseBinInventory> wareBin = warehouseBinDMapper.getWareBinOne(warehouseBinParam);
        return wareBin;
    }
}
